﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.EntitesDAL
{
    public class LophocDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_Select_Lophoc");
        }

        public DataTable Select(string id = null)
        {
            return ExecuteQuery("sp_SelectAll_Lophoc '" + id + "' ");
        }

        public DataTable Select_Khoa(string malop)
        {
            string sql = @"select MakhoaID from tb_Lophoc where Malop='{0}' ";
            sql = string.Format(sql, malop);
            return ExecuteQuery(sql);
        }
        public DataTable Select(string makhoa, string malop)
        {
            string sql = @"Select c.MaHV,c.Hoten,(Case when Gioitinh=0 then 'Nam' else N'Nữ' end) as Sex,c.Namsinh,c.Dienthoai,c.Diachi
                           From tb_Lophoc a join tb_Hocvien c on a.Malop=c.MalopID
                           Where a.MakhoaID='{0}' and a.Malop='{1}'   ";
            sql = string.Format(sql, makhoa, malop);
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Lophoc {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Lophoc)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Lophoc '{0}', '{1}', '{2}', N'{3}', '{4}', '{5}' ";
            sql = string.Format(sql,o.Malop, o.MakhoaID, o.MaloaiID, o.Tenlop, o.Soluong, o.Hocphi);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Lophoc)obj;
            var sql = "set dateformat dmy; execute sp_Update_Lophoc '{0}', '{1}', '{2}', N'{3}', '{4}', '{5}' ";
            sql = string.Format(sql, o.Malop, o.MakhoaID, o.MaloaiID, o.Tenlop, o.Soluong, o.Hocphi);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"select * from tb_Lophoc where Tenlop like N'%{0}%' ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search_A(DAL.Entities.Lophoc o)
        {
            string sql;
            if (o.Malop != "")
                sql = @"Select a.Malop, a.Tenlop, a.Soluong, a.Hocphi, a.MakhoaID, a.MaloaiID, b.Tenkhoa as Tenkhoa, c.Tenchungchi as Tenloai From tb_Lophoc a join tb_Khoahoc b on a.MakhoaID=b.Makhoa join tb_Loaichungchi c on a.MaloaiID=c.Maloai Where Malop like '%" + o.Malop + "%' and Tenlop like N'%" + o.Tenlop + "%' and Soluong like '%" + o.Soluong + "%' and Hocphi like '%" + o.Hocphi + "%'  ";
            else
                sql = @"Select a.Malop, a.Tenlop, a.Soluong, a.Hocphi, a.MakhoaID, a.MaloaiID, b.Tenkhoa as Tenkhoa, c.Tenchungchi as Tenloai From tb_Lophoc a join tb_Khoahoc b on a.MakhoaID=b.Makhoa join tb_Loaichungchi c on a.MaloaiID=c.Maloai Where Tenlop like N'%" + o.Tenlop + "%' and Soluong like '%" + o.Soluong + "%' and Hocphi like '%" + o.Hocphi + "%'  ";
            return ExecuteQuery(sql);
        }
       
        #endregion

        #region Other

        public DataTable Select_Tongso(string id)
        {
            string sql = @" Select MalopID,COUNT(maHV) as Tongso Into #tmp
                            From tb_hocvien a join tb_Lophoc b on a.MalopID=b.Malop
                            Where b.MakhoaID='{0}'
                            Group by MalopID

                            Select Malop,Tenlop,Hocphi,Soluong,Tongso,Soluong-Tongso as Conlai
                            From tb_Lophoc a join #tmp b on a.Malop=b.MalopID ";
            sql = string.Format(sql, id);
            return ExecuteQuery(sql);
        }

        public DataTable Select_TestRegister(string id1, string id2)
        {
            string sql = @" Select MalopID,COUNT(maHV) as Tongso Into #tmp
                            From tb_hocvien a join tb_Lophoc b on a.MalopID=b.Malop
                            Where b.MakhoaID='{0}'
                            Group by MalopID

                            Select Malop,Tenlop,Soluong,Tongso,Soluong-Tongso as Conlai
                            From tb_Lophoc a join #tmp b on a.Malop=b.MalopID
                            Where Malop = '{1}'   ";
            sql = string.Format(sql, id1, id2);
            return ExecuteQuery(sql);
        }

        #endregion
    }
}
